<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模态框</title>
    <style>
        .box {
            position: fixed;
            left: 50%;
            top: 50%;
            width: 200px;
            height: 200px;
            background-color: skyblue;
            margin: 0 auto 200px;
            box-shadow: 0 0 20px #ddd;
            transform: translate(-50%,-50%);
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        var box = document.querySelector('.box')
        // 鼠标按下 执行函数
        box.addEventListener('mousedown', function (e) {
            // 获取x 和 y坐标
            var x = e.pageX - box.offsetLeft;
            var y = e.pageY - box.offsetTop;
            // 鼠标移动 执行函数
            document.addEventListener('mousemove', move)
            function move(e) {
                // 将坐标赋值给box
                box.style.left = e.pageX - x +'px';
                box.style.top = e.pageY - y +'px';
            }
            // 鼠标松开 停止函数
            document.addEventListener('mouseup',function(){
                document.removeEventListener('mousemove',move)
            })

        })
    </script>
</body>

</html>